package com.weavernorth.humanresource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Encoder;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.Iterator;

/**
 * Created by guo_zy on 2019/12/30.
 * 转正
 */
public class personnelBecomeRegularWorker implements Action {
    public static Log log = LogFactory.getLog(personnelBecomeRegularWorker.class.getName());

    @Override
    public String execute(RequestInfo requestInfo) {
        String fhz = "";
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        SalarRangeUtil salarRangeUtil=new SalarRangeUtil();
        String requestid = requestInfo.getRequestid();//请求ID
        //String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        //提示信息
        String strMessage = "";

        String ygbh = "";//员工编号
        String zzrq = "";//转正日期
        String czlx = "Z2";//操作类型
        String czyy = "";//操作原因
        String gw = "";//岗位
        String rsfw = "";//人事范围
        String ygz = "";//员工组
        String ygzz = "";//员工子组
        String rszfw = "";//人事子范围
        String gzfw = "";//工资核算范围
        //转正日期
        String strZzrq="";

        // --zm修改 是否已推送SAP
        String strSapFlag = "";

        String sql = " select ygbh,zzrq,czyy,gw,rsfw,ygz,ygzz,rszfw,gzfw,sftssap,zzrq from " + tablename + " where requestid =" + requestid;
        rs.execute(sql);//确保只有1条记录
        if (rs.getCounts() != 1)
            return FAILURE_AND_CONTINUE;
        while (rs.next()) {
            ygbh = rs.getString("ygbh");//员工编号
            zzrq = rs.getString("zzrq");//转正日期
            //czyy = rs.getString("czyy");//操作原因
            switch (rs.getString("czyy")) {
                case "0":
                    czyy = "10";
                    break;
                case "1":
                    czyy = "20";
                    break;
                default:
                    czyy = "";
            }
            gw = rs.getString("gw");//岗位
            rsfw = rs.getString("rsfw");//人事范围
            ygz = rs.getString("ygz");//员工组
            ygzz = rs.getString("ygzz");//员工子组
            rszfw = rs.getString("rszfw");//人事子范围
            gzfw = rs.getString("gzfw");//工资核算范围
            strZzrq=rs.getString("zzrq");
            //是否已推送SAP
            strSapFlag = Util.null2String(rs.getString("sftssap"));
        }
        sql = "select jobtitlecode from hrmjobtitles where jobstatus ='1' and id = " + gw;
        rs.execute(sql);
        while (rs.next()) {
            gw = rs.getString("jobtitlecode");//岗位
        }
        sql = "select rsfwdm from uf_rsfw where id = " + rsfw;
        rs.execute(sql);
        while (rs.next()) {
            rsfw = rs.getString("rsfwdm");//人事范围
        }
        sql = "select ygzdm from uf_ygz where id = " + ygz;
        rs.execute(sql);
        while (rs.next()) {
            ygz = rs.getString("ygzdm");//员工组
        }
        sql = "select ygzzdm from uf_ygzz where id = " + ygzz;
        rs.execute(sql);
        while (rs.next()) {
            ygzz = rs.getString("ygzzdm");//员工子组
        }
        sql = "select rszfwdm from uf_rszfw where id = " + rszfw;
        rs.execute(sql);
        while (rs.next()) {
            rszfw = rs.getString("rszfwdm");//人事子范围
        }
        sql = "select gzhsfw from uf_gzfw where id = " + gzfw;
        rs.execute(sql);
        while (rs.next()) {
            gzfw = rs.getString("gzhsfw");//工资核算范围
        }
        //********************************************
        //zm 增加,判断工资范围是否锁定 开始

        String strLockMess = salarRangeUtil.judgeLock(gzfw, strZzrq);
        log.info("工资核算范围返回信息:" + strLockMess);
        if (!"".equals(strLockMess)) {
            strLockMess=strLockMess+"薪资核算结束后，工资范围解除锁定，流程可以继续提交";
            requestInfo.getRequestManager().setMessagecontent(strLockMess);
            return FAILURE_AND_CONTINUE;

        }
        //zm 增加,判断工资范围是否锁定 结束



        StringBuffer xml = new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">");
        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body><urn:ZHRI_079>");
        xml.append("<I_ABKRS>" + gzfw + "</I_ABKRS>");
        xml.append("<I_BEGDA>" + zzrq + "</I_BEGDA>");
        xml.append("<I_BTRTL>" + rszfw + "</I_BTRTL>");
        xml.append("<I_MASSG>" + czyy + "</I_MASSG>");
        xml.append("<I_MASSN>" + czlx + "</I_MASSN>");
        xml.append("<I_PERNR>" + ygbh + "</I_PERNR>");
        xml.append("<I_PERSG>" + ygz + "</I_PERSG>");
        xml.append("<I_PERSK>" + ygzz + "</I_PERSK>");
        xml.append("<I_PLANS>" + gw + "</I_PLANS>");
        xml.append("<I_WERKS>" + rsfw + "</I_WERKS>");
        xml.append("<RETURN><item>");
        xml.append("<TYPE></TYPE>");
        xml.append("<ID></ID>");
        xml.append("<NUMBER></NUMBER>");
        xml.append("<MESSAGE></MESSAGE>");
        xml.append("<LOG_NO></LOG_NO>");
        xml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        xml.append("<MESSAGE_V1></MESSAGE_V1><MESSAGE_V2></MESSAGE_V2><MESSAGE_V3></MESSAGE_V3><MESSAGE_V4></MESSAGE_V4>");
        xml.append("<PARAMETER></PARAMETER>");
        xml.append("<ROW></ROW>");
        xml.append("<FIELD></FIELD>");
        xml.append("<SYSTEM></SYSTEM>");
        xml.append("</item></RETURN>");
        xml.append("</urn:ZHRI_079></soapenv:Body>");
        xml.append("</soapenv:Envelope>");
        log.info("转正推送xml:" + xml.toString());

        //--zm修改 如果推送过SAP则不需再次推送SAP
        if (!"0".equals(strSapFlag)) {
            try {
                String zzUrl = bb.getPropValue("personnel-erdos", "zzurl");//获取配置文件信息
                String responseXml = salarRangeUtil.sendPost(zzUrl, xml.toString(), "utf-8");
                log.info("zhuanzhengdefanhuizhi:" + responseXml);//打印日志
                Document doc = null;//解析返回值开始
                try {
                    doc = DocumentHelper.parseText(responseXml);
                    Iterator<Element> its2 = doc.selectNodes("//TYPE").iterator();
                    while (its2.hasNext()) {
                        Element element = its2.next();
                        fhz = element.getText();
                        if (!"".equals(fhz)) {
                            break;
                        }
                    }

                    bb.writeLog("返回值:" + fhz);

                    //返回值失败时,则提示失败
                    if ("E".equals(fhz)) {
                        Iterator<Element> its3 = doc.selectNodes("//MESSAGE").iterator();
                        while (its3.hasNext()) {
                            Element element2 = its3.next();
                            strMessage = element2.getText();
                            if (!"".equals(strMessage)) {
                                break;
                            }
                        }

                    }
                    bb.writeLog("返回信息:" + strMessage);
                } catch (DocumentException e) {
                    e.printStackTrace();
                }//解析结束
            } catch (Exception e) {
                e.printStackTrace();
            }
            //--zm修改推送过则直接更新流程
        } else {
            fhz = "S";
        }
        if ("S".equals(fhz)) {

            return SUCCESS;
        } else {
            if (!"".equals(strMessage)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
            }
            return FAILURE_AND_CONTINUE;
        }
    }


}
